﻿Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Input
Imports GalgameParser
Imports System.Resources
Imports System.Reflection
Imports System.IO.IsolatedStorage
Imports System.IO

Partial Public Class GamePage
    Inherits PhoneApplicationPage

    Private contentManager As IsolatedContentManager
    Private timer As GameTimer

    Private m_gameParser As GalgameParser.GalgameParser
    Private m_scriptOnDraw As ScriptTask
    Private m_scriptOnUpdate As ScriptTask
    Private m_scriptOnBackKeyPress As ScriptTask
    Private m_fileStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()

    Public Sub New()
        InitializeComponent()

        ' Create a timer for this page
        timer = New GameTimer()
        timer.UpdateInterval = TimeSpan.FromTicks(333333)
        AddHandler timer.Update, AddressOf OnUpdate
        AddHandler timer.Draw, AddressOf OnDraw
    End Sub

    ''' <summary>
    ''' Equals to the XNA event LoadContent.
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
        Dim parameters = Me.NavigationContext.QueryString
        If parameters.ContainsKey("GameName") Then
            Me.m_gameParser = g_gameList.Where(Function(obj) obj.Name = Uri.UnescapeDataString(parameters("GameName")))(0)
        Else
            Throw New ArgumentException("GameName parameter is missing.")
        End If

        ' Set the sharing mode of the graphics device to turn on XNA rendering
        SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(True)

        Me.contentManager = New IsolatedContentManager(DirectCast(Application.Current, App).Services, m_gameParser.GameContentRoot)

        Dim scriptInitialization As New ScriptTask("Initialization.rb")
        scriptInitialization.LoadScriptFromIsolatedStorage(IO.Path.Combine(Me.contentManager.IsolatedStorageRootDirectory, "Initialization.rb"), m_fileStorage)

        scriptInitialization.LoadAssembly(Me.m_gameParser.GetType().Assembly)
        scriptInitialization.LoadAssembly(GetType(SpriteFont).Assembly)
        scriptInitialization.LoadAssembly(GetType(SpriteBatch).Assembly)
        scriptInitialization.LoadAssembly(GetType(GraphicsDevice).Assembly)
        scriptInitialization.LoadAssembly(GetType(Texture2D).Assembly)
        scriptInitialization.LoadAssembly(GetType(Vector2).Assembly)

        scriptInitialization.Excute( _
            New Tuple(Of String, Object)("ThisGameParser", Me.m_gameParser), _
            New Tuple(Of String, Object)("PhonePage", Me), _
            New Tuple(Of String, Object)("ContentManager", Me.contentManager), _
            New Tuple(Of String, Object)("CurrentGraphicsDevice", SharedGraphicsDeviceManager.Current.GraphicsDevice))

        Me.m_scriptOnDraw = New ScriptTask("OnDraw.rb")
        m_scriptOnDraw.LoadScriptFromIsolatedStorage(IO.Path.Combine(Me.contentManager.IsolatedStorageRootDirectory, "OnDraw.rb"), m_fileStorage)

        Me.m_scriptOnDraw.LoadAssembly(Me.m_gameParser.GetType().Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(SpriteFont).Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(SpriteBatch).Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(GraphicsDevice).Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(Vector2).Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(Texture2D).Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(GameTimerEventArgs).Assembly)

        Me.m_scriptOnUpdate = New ScriptTask("OnUpdate.rb")
        m_scriptOnUpdate.LoadScriptFromIsolatedStorage(IO.Path.Combine(Me.contentManager.IsolatedStorageRootDirectory, "OnUpdate.rb"), m_fileStorage)

        Me.m_scriptOnUpdate.LoadAssembly(Me.m_gameParser.GetType().Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(SpriteFont).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(SpriteBatch).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(GraphicsDevice).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(Vector2).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(Texture2D).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(GamePad).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(GameTimerEventArgs).Assembly)

        Me.m_scriptOnBackKeyPress = New ScriptTask("OnBackKeyPress.rb")
        m_scriptOnBackKeyPress.LoadScriptFromIsolatedStorage(IO.Path.Combine(Me.contentManager.IsolatedStorageRootDirectory, "OnBackKeyPress.rb"), m_fileStorage)

        Me.m_scriptOnBackKeyPress.LoadAssembly(Me.m_gameParser.GetType().Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(SpriteFont).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(SpriteBatch).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(GraphicsDevice).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(Vector2).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(Texture2D).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(GamePad).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(GameTimerEventArgs).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(MessageBox).Assembly)

        ' Start the timer
        timer.UpdateInterval = TimeSpan.FromTicks(Me.m_gameParser.Fps)
        timer.Start()

        ' Set the screen orientation.
        Me.Orientation = System.Enum.Parse(Me.Orientation.GetType(), Me.m_gameParser.Orientation, True)

        MyBase.OnNavigatedTo(e)
    End Sub

    ''' <summary>
    ''' Leave the page means stopping the game.
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnNavigatedFrom(e As NavigationEventArgs)
        ' Stop the timer
        timer.Stop()

        ' Set the sharing mode of the graphics device to turn off XNA rendering
        SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(False)

        MyBase.OnNavigatedFrom(e)
    End Sub

    ''' <summary>
    ''' Allows the page to run logic such as updating the world,
    ''' checking for collisions, gathering input, and playing audio.
    ''' </summary>
    Private Sub OnUpdate(sender As Object, e As GameTimerEventArgs)
        ' TODO: Add your update logic here.
        Me.m_scriptOnUpdate.LoadAssembly(e.GetType().Assembly)
        Me.m_scriptOnUpdate.Excute( _
            New Tuple(Of String, Object)("ThisGameParser", Me.m_gameParser), _
            New Tuple(Of String, Object)("PhonePage", Me), _
            New Tuple(Of String, Object)("GameTimerEventArg", e), _
            New Tuple(Of String, Object)("CurrentGraphicsDevice", SharedGraphicsDeviceManager.Current.GraphicsDevice))
    End Sub

    ''' <summary>
    ''' Allows the page to draw itself.
    ''' </summary>
    Private Sub OnDraw(sender As Object, e As GameTimerEventArgs)
        ' TODO: Add your drawing code here        
        Me.m_scriptOnDraw.LoadAssembly(e.GetType().Assembly)
        Me.m_scriptOnDraw.Excute( _
            New Tuple(Of String, Object)("ThisGameParser", Me.m_gameParser), _
            New Tuple(Of String, Object)("PhonePage", Me), _
            New Tuple(Of String, Object)("GameTimerEventArg", e), _
            New Tuple(Of String, Object)("CurrentGraphicsDevice", SharedGraphicsDeviceManager.Current.GraphicsDevice))
    End Sub

    Protected Overrides Sub OnBackKeyPress(e As System.ComponentModel.CancelEventArgs)        
        Me.m_scriptOnBackKeyPress.LoadAssembly(e.GetType().Assembly)
        Me.m_scriptOnBackKeyPress.Excute( _
            New Tuple(Of String, Object)("ThisGameParser", Me.m_gameParser), _
            New Tuple(Of String, Object)("PhonePage", Me), _
            New Tuple(Of String, Object)("CancelEventArg", e), _
            New Tuple(Of String, Object)("CurrentGraphicsDevice", SharedGraphicsDeviceManager.Current.GraphicsDevice))
    End Sub

End Class